<?php

namespace app\common\model;

use think\Model;


class Wechat Extends Model{


    static function getAccessToken() {
        //        $token = redis('wx_access_token'); 
        //        if (empty($token)) {
        $tokenInfo = curl_get('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . Config('wx.mini_appid') . '&secret=' . Config('wx.mini_secret'));
        $tokenInfo = json_decode($tokenInfo, true);
        $token = $tokenInfo['access_token'];
        //dump($tokenInfo);
        //redis('wx_access_token', $token, $tokenInfo['expires_in']);
        //        }
        return $token;
    }

    static function getPhone($code)
    {
        $param['code'] = $code;
        $token = self::getAccessToken();
        $url = 'https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token='.$token;
        $ret = curl_post($url,json_encode($param),array("Content-Type:application/json"));
        return json_decode($ret,true);
    }

    static function getJsApiTicket() {
        $ticket = redis('wx_js_api_ticket');
        if (empty($ticket)) {
            $ticketInfo = curl_get('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . self::getAccessToken() . '&type=jsapi');
            $ticketInfo = json_decode($ticketInfo, true);
            $ticket = $ticketInfo['ticket'];
            redis('wx_js_api_ticket', $ticket, $ticketInfo['expires_in']);
        }
        return $ticket;
    }

    static function isWechat() {
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
            return true;
        }
        return false;
    }

    static function getJsApiSign() {
        $wxData = array(
            'noncestr' => create_password(),
            'jsapi_ticket' => WechatModel::getJsApiTicket(),
            'timestamp' => time(),
            'url' => 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'],
        );
        ksort($wxData);
        $wSign = http_build_query($wxData);
        $wSign = urldecode($wSign);
        $wxData['sign'] = sha1($wSign);
        unset($wxData['jsapi_ticket']);
        return $wxData;
    }

    static function getOpenId($code) {
        //$data = curl_get("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . Config('wx.mini_appid') . "&secret=" . Config('wx.mini_secret') . "&code=" . $code . "&grant_type=authorization_code");
        $data = curl_get("https://api.weixin.qq.com/sns/jscode2session?appid=" . Config('wx.mini_appid') . "&secret=" . Config('wx.mini_secret') . "&js_code=" . $code . "&grant_type=authorization_code");
        $data = json_decode($data, true);
        return $data;
    }

    static function getPaySign($package = "") {
        $data = array(
            'appId' => C('wxpay.appid'),
            'timeStamp' => time(),
            'nonceStr' => create_password(32),
            'package' => 'prepay_id=' . $package,
            'signType' => 'MD5'
        );
        ksort($data);
        $wSign = array_filter($data);
        $wSign = http_build_query($wSign);
        $wSign = urldecode($wSign);
        $wSign .= "&key=" . C('wxpay.key');
        $mySign = strtoupper(md5($wSign));
        $data['paySign'] = $mySign;
        return $data;
    }


    public static function decryptData($encryptedData, $iv, $session_key) {
        $aesKey = base64_decode($session_key);
        $aesIV = base64_decode($iv);
        $aesCipher = base64_decode($encryptedData);
        $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
        $dataObj = json_decode($result);
        return $dataObj;
    }
}